<?php

namespace App\Http\Validate\OpenShop\InvestmentPromotion;

use App\Enums\OpenShop\InvestmentPromotion\UserOrderEnum;
use App\Http\Validate\BaseValidate;
use App\Models\Kms\User\KmsUser;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopUserContractModel;
use App\Support\Facade\CustomerValidator;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;

/**
 * [ 订单业务验证 ] - 校验规则
 *
 * @OpenShopUserContractValidate
 * @package App\Http\Controllers
 * @date 2022/03/01 02:35:56
 * @author GreenLeaf <786407418@qq.com>
 */
class OrganizationValidate extends BaseValidate
{


    static function registerValidateRules(): void
    {
        // TODO: Implement registerValidateRules() method.
        Validator::extend(
            'sizeEq',
            function ($attribute, $value, $parameters, \Illuminate\Validation\Validator $validator) {
                $validator->requireParameterCount(1, $parameters, 'sizeEq');
                $sizeEqFieldValue = $validator->getData()[$parameters[0]];
                return count($value) == count($sizeEqFieldValue);
            }, "营运附属负责人参数有误");
    }

    static function getRules(array $fields = []): array
    {
        return self::getActuallyRules([
            'type'                   => "required|integer|min:1",
            'manageId'               => "required|integer|min:1",
            'name'                   => "required|string",
            'developName'            => "required|string",
            'developUid'             => "required|integer|min:1",
            'preparationName'        => "required|string",
            'preparationUid'         => "required|integer|min:1",
            'designerName'           => "",
            'designerUid'            => "",
            'operationName'          => "required|string",
            'operationUid'           => "required|integer|min:1",
            'mappingData'            => "array",
            'operationAttacheUid'    => "array|sizeEq:operationAttacheName",
            'operationAttacheUid.*'  => "int|min:1",
            "operationAttacheName"   => "array",
            "operationAttacheName.*" => "string|min:1",
            "attendanceUser"         => "array|min:0",
            "attendanceUser.*.uid"   => [
                "required",
                "int",
                "min:1",
//                Rule::exists((new KmsUser())->getTable(), "rmsUserId")
            ],
            "attendanceUser.*.name"  => [
                "required",
                "string",
                "min:1"
            ],

            'cityManagers'                                  => 'array',
            'cityManagers.*.userId'                         => 'required|integer',
            'cityManagers.*.addressCodesArr'                => 'required|array',
            'cityManagers.*.addressCodesArr.*.provinceCode' => 'required',
            'cityManagers.*.addressCodesArr.*.cityCode'     => 'required',


//            "provinceDevelopmentUser"        => "array|min:0",
//            "provinceDevelopmentUser.*.uid"  => [
//                "required",
//                "int",
//                "min:1",
//            ],
//            "provinceDevelopmentUser.*.name" => [
//                "required",
//                "string",
//                "min:1"
//            ],

//            'siteSelectionDevelopments'                                  => 'array',
//            'siteSelectionDevelopments.*.userId'                         => 'required|integer',
//            'siteSelectionDevelopments.*.addressCodesArr'                => 'required|array',
//            'siteSelectionDevelopments.*.addressCodesArr.*.provinceCode' => 'required',
//            'siteSelectionDevelopments.*.addressCodesArr.*.cityCode'     => 'required',
        ], $fields);

    }

    public static array $attributes = [
        'type'            => '组织类型',
        'manageId'        => '组织ID',
        'name'            => '组织名称',
        'developName'     => '开发负责人',
        'preparationName' => '筹备负责人',
        'mappingData'     => '区域关联信息',
        'developUid'      => '开发负责人id',
        'preparationUid'  => '筹备负责人id',
        'designerUid'     => '设计师id',
        'designerName'    => '设计师姓名',
        'operationUid'    => '营运负责人uid',
        'operationName'   => '营运负责人姓名',
        'uid'             => "用户uid",
        "attendanceUser"  => "考勤用户信息"
    ];

    public static function updateValidate(array $requestParams): array
    {
        return self::baseValidate(
            $requestParams,
            array_keys(self::getRules()),
            function ($params) {
                $params["operationAttacheUser"] = (function ($params) {
                    $operationAttacheUser = [];
                    foreach ($params["operationAttacheUid"] as $key => $uid) {
                        array_push($operationAttacheUser, [
                            "uid"  => $uid,
                            "name" => $params["operationAttacheName"][$key]
                        ]);
                    }
                    return json_encode($operationAttacheUser, JSON_UNESCAPED_UNICODE);
                })($params);

                unset($params["operationAttacheUid"], $params["operationAttacheName"]);
                if (!isset($params["attendanceUser"])) {
                    $params["attendanceUser"] = "";
                }
                return $params;
            },
            null,
            self::getValidateMsg()
        );
    }


    /**
     * @param array $requestParams
     * @return array
     * @throws ValidationException
     */
    public static function userOrganizationValidate(array $requestParams): array
    {
        return self::baseValidate(
            $requestParams,
            ["uid"],
            null,
            ["uid" => "required|int|min:1"],
            null
        );
    }

}
